<?php

namespace Home\Controller;
use OT\DataDictionary;

/**
 *
 *
 */
class MineController extends HomeController {

    /*
     * 发送短信接口
     *
     * */
    public function authcode(){

        $resu = array('code' => 1,'msg' => '','data' => array());

        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $auth = rand(100000,999999);//定义一个随机六位数的变量
        $smsConf = array(
            'key'   => '5ff30d5a8e99e043e3a245d69810b7dd', //您申请的APPKEY
            'mobile'    => $_POST['mobile'], //接受短信的用户手机号码
            'tpl_id'    => '98177', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => '#code#='.$auth //您设置的模板变量，根据实际情况修改
        );

        $dt = M('personal');
        $data['auth_code'] = $auth;
        $data['mobile'] = $_POST['mobile'];
        $where = array('mobile' => $_POST['mobile']);
        $list = $dt->where($where)->find();
        if(empty($list)){
            $dt->add($data);
        }else{
            $dt->where($where)->save($data);
        }
        $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
        if($content){
            $result = json_decode($content,true);
            $error_code = $result['error_code'];
            if($error_code == 0){//状态为0，说明短信发送成功
                $resu['msg'] = '短信发送成功!';
                $this -> assign($resu);
            }else{//状态非0，说明失败
                $msg = $result['reason'];
                $resu['code'] = 2;
                $resu['msg'] = '短信发送失败';
                $this -> assign($resu);
            }
        }else {
            $resu['code'] = 2;
            $resu['msg'] = '请求发送短信发送失败';
            $this -> assign($resu);
        }
        $this -> display();
    }
    /*
     * 登录页面接口
     *
     * @params varchar mobile     电话号码
     * @params varchar password   密码
     * */
    public function login(){

        $resu = array('code' => 1,'msg' => '登陆成功','data' => array());
        $mobile = $_POST['mobile'];
        $auth = $_POST['password'];
        $dt = M('personal');
        $where = array('mobile' => $mobile);

        $token = array(//接受电话号码并且创建token登陆时间和退出时间
            'mobile' => $_POST['mobile'],
            'ctime' => time(),
            'out_time' => (time()+36000*24*7)
        );
        $toke = base64_encode(json_encode($token));//加密token
        $data['ctime'] = $token['ctime'];
        $data['token'] = $toke;
        $arr = array('token' => $data['token']);

        $list_mobile = $dt -> where($where)->field('mobile')->find();
        $listb_password = $dt -> where($where)->field('password')->find();

        $pwd_mobile = implode($list_mobile);
        $pwd_password = implode($listb_password);//转换密码和手机号码类型

        if($pwd_mobile == $mobile){
            if($auth == $pwd_password){
                $dt -> where($where)->save($data);
                $resu['data'] = $toke;
            }else{
                $resu['msg'] = '密码错误!';
            }
        }else{
            $resu['msg'] = '手机号码错误!';
        }
        $this -> ajaxReturn($resu);
    }

    /*
     * 注册页面接口
     *
     * */
    public function register(){
        $resu = array('code' => 1,'msg' => '注册成功');
        $mobile = $_POST['mobile'];
        $auth = $_POST['pwd'];
        $password = $_POST['password'];
        $dt = M('personal');
        $where = array('mobile' => $mobile);
        $array = array('password' => $password,'auth_code' => '----------');//储存密码和清空验证码为'----------'

        $authcode = $dt->where($where)->field('auth_code')->find();
        $yanzheng = implode($authcode);//转换验证码类型
        if($auth == $yanzheng){
            $dt->where($where)->save($array);
        }else{
            $resu['msg'] = '验证码错误!';
        }
        $this -> ajaxReturn($resu);
    }

}